package cn.eleven.gui;

import cn.eleven.entity.User;
import cn.eleven.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;

/**
 * 个人中心界面
 */
public class ProfileUI extends JPanel {
    private final UserService userService = new UserService(); // 用户服务类
    private final int currentUserId; // 当前用户的ID

    public ProfileUI(int currentUserId) throws SQLException {
        this.currentUserId = currentUserId;
        initializeUI(); // 初始化界面
    }

    /**
     * 初始化界面布局和组件
     * @throws SQLException 数据库异常
     */
    private void initializeUI() throws SQLException {
        setLayout(new BorderLayout());
        setBackground(new Color(240, 240, 240)); // 设置背景色

        // 获取当前用户信息
        User user = userService.findUserById(currentUserId);
        if (user == null) {
            JOptionPane.showMessageDialog(this, "用户信息加载失败");
            return;
        }

        // 创建卡片面板
        JPanel cardPanel = new JPanel();
        cardPanel.setLayout(new GridLayout(3, 2, 10, 10));
        cardPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("个人信息"),
                BorderFactory.createEmptyBorder(10, 10, 10, 10)
        ));
        cardPanel.setBackground(Color.WHITE);

        // 用户ID
        JLabel idLabel = new JLabel("用户ID:");
        idLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        JTextField idField = new JTextField(String.valueOf(user.getId()));
        idField.setEditable(false);
        idField.setBackground(new Color(250, 250, 250));
        cardPanel.add(idLabel);
        cardPanel.add(idField);

        // 用户名
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        JTextField usernameField = new JTextField(user.getUsername());
        usernameField.setEditable(false);
        usernameField.setBackground(new Color(250, 250, 250));
        cardPanel.add(usernameLabel);
        cardPanel.add(usernameField);

        // 角色
        JLabel roleLabel = new JLabel("角色:");
        roleLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        JTextField roleField = new JTextField(user.getRole());
        roleField.setEditable(false);
        roleField.setBackground(new Color(250, 250, 250));
        cardPanel.add(roleLabel);
        cardPanel.add(roleField);

        // 将卡片面板居中显示
        JPanel centerPanel = new JPanel(new GridBagLayout());
        centerPanel.setBackground(new Color(240, 240, 240));
        centerPanel.add(cardPanel);

        add(centerPanel, BorderLayout.CENTER);
    }
}